//
//  ECUIVC_Category.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/7.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUIVC_Category.h"
#import "ECUIView_Category.h"
#import "ECUIView_TVSelected.h"
#import "ECModelData.h"
#import "HWHttpTool.h"
#import "ECUIVC_CourseList.h"
#import "ECUIView_HomeListNoData.h"
#import "UIImage+RenderedImage.h"

const CGFloat categoryViewAnimationDuring = 1;
@interface ECUIVC_Category ()
{
    NSURLSessionDataTask *task;
}
@property(nonatomic,strong) ECUIView_Category *categoryView;
@property(nonatomic,strong) NSMutableArray *categoryListArr;
@property(nonatomic,strong)ECUIView_HomeListNoData *nodataView;


@end

@implementation ECUIVC_Category

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestData];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIColor *color = [UIColor colorWithHexString:@"#f7f7f7"];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[[UIColor  darkGrayColor] colorWithAlphaComponent:1] renderSize:CGSizeMake(kScreenWidth, 0.25)];
    if (task && task.state == NSURLSessionTaskStateCompleted) {
        if (self.categoryListArr.count <= 0) {
            [self requestData];
        }
    }
}
- (ECUIView_HomeListNoData *)nodataView
{
    if (!_nodataView) {
        _nodataView = [[ECUIView_HomeListNoData alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavHeight - kTabbarHeight)];
        _nodataView.imageView.image = [UIImage imageNamed:@"image_list_nodata"];
        _nodataView.backgroundColor = [UIColor whiteColor];

    }
    _nodataView.textLabel.text = @"分类请求失败";
    return _nodataView;
}
- (void)showNodataView:(BOOL)flag {
    if (flag) {
        [self.categoryView addSubview:self.nodataView];
    } else {
        [self.nodataView removeFromSuperview];
        
    }
}
- (void)addSubviews{
    [super addSubviews];
    [self.view addSubview:self.categoryView];
}
-(void)requestData{
    WeakSelf(weakSelf);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self showNodataView:NO];
  task = [HWHttpTool get:URL_AllCategory params:@{} success:^(id json) {
        [MBProgressHUD hideHUD];

     weakSelf.categoryView.categorysData = [ECModelData arrayForAllCategory:json];
//    weakSelf.categoryView.defaultCategoryModel = [[ECModelData arrayForAllCategory:json] objectAtIndex:0];
        weakSelf.categoryListArr = [ECModelData arrayForCategory:json];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [weakSelf showNodataView:YES];
    }];
}
- (ECUIView_Category *)categoryView
{
    if (!_categoryView) {
        _categoryView = [[ECUIView_Category alloc]initWithFrame:self.view.bounds];
        _categoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_categoryView];
        WeakSelf(weakSelf);
        _categoryView.block_selectedAtIndex = ^(id obj, NSIndexPath *indexPath) {
            [weakSelf showListControllerWithCategory:obj];
        };
    }
    return _categoryView;
}

- (void)showListControllerWithCategory:(PrimaryCategoryModel *)mo{
    
    ECUIVC_CourseList *uivc_list = [[ECUIVC_CourseList alloc]init];
    uivc_list.object = mo;
    uivc_list.title = @"课程列表";//mo.title;
    // 这里需要判断一下全部与上一级的关系。
    PrimaryCategoryModel *defaultSelectMo = _categoryView.secondCategoryModel ? _categoryView.secondCategoryModel:_categoryView.firstCategoryModel;
    if ([mo.modelId isEqualToString: defaultSelectMo.modelId]) {
        mo = defaultSelectMo;
    }
    uivc_list.selectCategory = mo;
    uivc_list.categorysData = self.categoryListArr;
    [self.navigationController pushViewController:uivc_list animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
